
var Ajax={
    send:function(url,data,type,successfn,errorfn){
        layer.msg('数据提交中...',{time:500000});
        errorfn=errorfn || function(d){
			layer.msg(d.msg,{icon:2});
        }
        successfn=successfn || function(d){
			layer.msg(d.msg, {icon:1},function() {
				if ((d.url).length>0){location.href= d.url;}
			});
        }
        $.ajax({
            url: url,
            type:type,
            dataType: 'json',
            data:data || {},
            success:function(d){
                if(d.code==1){successfn(d);}else{errorfn(d);}
            },
            error:function(d){
				layer.msg('服务器繁忙,请稍后再试!',{icon:2});
            }
        })
    },
    get:function(url,data,success,error){
        Ajax.send(url,data,'GET',success,error);
    },
    post:function(url,data,success,error){
        Ajax.send(url,data,'POST',success,error);
    },
    sendForm:function(f,success,error){
        Ajax.send($(f).attr('action'),$(f).serialize(),($(f).attr('method') || 'GET'),success,error);
    }
}
function info (a){
	console.info(a);
}
var SyncFormData = function(f,ev){
	f= $(f || 'form');
	ev= $(ev || '#Editvo');
	var vo = $.parseJSON(ev.html());
	if(typeof FilterEditVo=="function"){
		vo = FilterEditVo(vo);
	}
	if(vo.length==0){return '';}
	//console.info(vo);
	f.find(':text,:password,textarea,input[type=hidden]').each(function(){
		var k = $(this).attr('name');
		if(vo[k]){
			$(this).val(vo[k]);
		}
	});
	f.find(':radio').each(function(){
		var k = $(this).attr('name');
		var val = vo[k]+"";
		if(val){
			f.find('[name='+k+']').removeAttr('checked');
			f.find('[name='+k+'][value='+val+']').attr('checked','checked');
		}
	});
	f.find(':checkbox').each(function(){
		var k = $(this).attr('name');
		var name = k.replace('[','').replace(']','');
		var val = vo[name]+"";
		if(val!=""){
			if(val.indexOf(',')>=0){
				val = val.split(',');
				for(var i in val){
					if($(this).val()==val[i]){
						$(this).attr('checked','checked');						
					}else{
						$(this).removeAttr('checked');	
					}
				}
			}else{
				if($(this).val()==val){
					$(this).attr('checked','checked');						
				}else{
					$(this).removeAttr('checked');	
				}				
			}
		}
	});
	f.find('select').each(function(){
		var k = $(this).attr('name');
		var val = vo[k]+"";
		if(val!=""){
			$(this).find('option[value='+val+']').attr('selected','selected');
		}
	});
	layui.use('form', function(){
		var form = layui.form;
		form.render();
		//各种基于事件的操作，下面会有进一步介绍
	});
}
function popUrl(url){
	return layer.open({
	  type: 2,
	  shade: false,
	  area: ['80%','80%'],
	  maxmin: true,
	  content: url,
	  zIndex: layer.zIndex, //重点1
	  success: function(layero){
	    layer.setTop(layero); //重点2
	  }
	});    
}
function edit(id){
	var url = '/'+CONTROLLER_NAME + '/add.html';
	if(id) {url = '/'+CONTROLLER_NAME + '/edit/id/'+id+'.html';}
	//通过这种方式弹出的层，每当它被选择，就会置顶。
	var editL = popUrl(url);     
}
function add(){
	edit(null);
}